#设置各种参数
#typeset MIP='127.0.0.1'
#typeset dbmpassword='123456'
# typeset MIP='10.27.3.175'               #主服务器ip
# typeset hmuser='root'                   #主服务器远程用户
# typeset hmpassword='1qaz@WSX'           #主服务器远程密码
# typeset hsuser='root'                   #从服务器远程用户
# typeset hspassword='1qaz@WSX'           #从服务器远程密码
# typeset dbmuser='root'                  #主服务器mysql用户
# typeset dbmpassword='QWEasdzxc#123'     #主服务器mysql密码
typeset SIP='@@SIP@@'               #从服务器ip
typeset dbsuser='@@dbsuser@@'                  #从服务器mysql用户
typeset dbspassword='@@dbspassword@@'     #从服务器mysql密码
typeset check_times='@@check_times@@'                  #命令重复次数
typeset mconfpath='@@mconfpath@@'         #主mysql配置文件路径
typeset sconfpath='@@sconfpath@@'         #从mysql配置文件路径

typeset SIP='10.27.3.176'               #从服务器ip
typeset dbsuser='root'                  #从服务器mysql用户
typeset dbspassword='QWEasdzxc#123'     #从服务器mysql密码
typeset check_times=''                  #命令重复次数
typeset mconfpath='/etc/my.cnf'         #主mysql配置文件路径
typeset sconfpath='/etc/my.cnf'         #从mysql配置文件路径
startok=false

#检查check次数
[ ! ${check_times} ] && typeset check_times='3'
expr 1 + ${check_times} &>/dev/null
if [ $? -ne 0 ];then
    log_format "ERROR" 'Argument needs to be integer' && log_format "ERROR" "script_execute_error!" && exit 1
fi

#配置日志路径
typeset SCRIPT_NAME="mysql_slave_$SIP"
typeset LOG_PATH='/tmp/dr_log'
[ ! -d "${LOG_PATH}" ] && mkdir -p ${LOG_PATH}
typeset LOG_FILE="${LOG_PATH}/${SCRIPT_NAME}.log"
[ ! -f "${LOG_FILE}" ] && touch ${LOG_FILE}

function log_format()
{
    echo "[`date +"%F %H:%M:%S"`] [$*]" | tee -a ${LOG_FILE}
}

# log_format "begin to build mysql master................................."

#检查配置文件是否存在
if [ ! "$mconfpath" ];then
    cnfnum=`find / -name my.cnf|wc -l`
    if [ "$cnfnum" -gt 1 ];then
        log_format 'ERROR' 'number of my.cnf is more than one,please input my.cnf path' && log_format "ERROR" "script_execute_error!" && exit 1
    else
        mconfpath=`find / -name my.cnf `
        log_format 'INFO' "find my.cnf path: $mconfpath"
    fi
else
    if [ ! -f "$mconfpath" ];then
        log_format 'ERROR' "my.cnf does not exit,check my.cnf path" && log_format "ERROR" "script_execute_error!" && exit 1
    # else
    #     log_format 'INFO' "my.cnf exits"
    fi
fi

#检查mysql命令路径
basedir=`cat $mconfpath|grep -v ^#|grep basedir|awk -F'=' '{print $2}'| sed -e 's/^[ ]*//g' | sed -e 's/[ ]*$//g'`
if [ ! "$basedir" ];then
    log_format 'ERROR' "can not find basedir" && log_format "ERROR" "script_execute_error!" && exit 2
else  
    mycmdpath=$basedir/bin/mysql
    if [ ! -e "$mycmdpath" ];then
        log_format 'ERROR' "can not find mysql cmd $mycmdpath" && log_format "ERROR" "script_execute_error!" && exit 2
    # else
    #     log_format 'INFO' "mysql cmd finded $mycmdpath"
    fi
fi

mycmd="$mycmdpath -h$SIP -u$dbsuser -p$dbspassword"
getok=false
for ((i=0;i<${check_times};i++))
do
    sleep 5
    $mycmd -e "show slave status\G" 2> /dev/null
    if [[ $? -eq 0  ]];then
        getok=true
        break
    # else
    #     log_format "MARNING" "fali to get status,retry "
    fi
done
if [ $getok == false ];then
    log_format "ERROR" "faild to get status! && log_format "ERROR" "script_execute_error!" && exit 1"
# else
#     log_format "INFO" "success to get status!"
fi

log_format "INFO" "script_execute_success!"